package com.ruoyi.peizhen.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.peizhen.domain.HospitalDepartment;
import com.ruoyi.peizhen.service.IHospitalDepartmentService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.Ztree;

/**
 * 科室Controller
 * 
 * @author ruoyi
 * @date 2022-03-13
 */
@Controller
@RequestMapping("/peizhen/department")
public class HospitalDepartmentController extends BaseController
{
    private String prefix = "peizhen/department";

    @Autowired
    private IHospitalDepartmentService hospitalDepartmentService;

    @RequiresPermissions("peizhen:department:view")
    @GetMapping()
    public String department()
    {
        return prefix + "/department";
    }

    /**
     * 查询科室树列表
     */
    @RequiresPermissions("peizhen:department:list")
    @PostMapping("/list")
    @ResponseBody
    public List<HospitalDepartment> list(HospitalDepartment hospitalDepartment)
    {
        List<HospitalDepartment> list = hospitalDepartmentService.selectHospitalDepartmentList(hospitalDepartment);
        return list;
    }

    /**
     * 导出科室列表
     */
    @RequiresPermissions("peizhen:department:export")
    @Log(title = "科室", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(HospitalDepartment hospitalDepartment)
    {
        List<HospitalDepartment> list = hospitalDepartmentService.selectHospitalDepartmentList(hospitalDepartment);
        ExcelUtil<HospitalDepartment> util = new ExcelUtil<HospitalDepartment>(HospitalDepartment.class);
        return util.exportExcel(list, "科室数据");
    }

    /**
     * 新增科室
     */
    @GetMapping(value = { "/add/{id}", "/add/" })
    public String add(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("hospitalDepartment", hospitalDepartmentService.selectHospitalDepartmentById(id));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存科室
     */
    @RequiresPermissions("peizhen:department:add")
    @Log(title = "科室", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(HospitalDepartment hospitalDepartment)
    {
        return toAjax(hospitalDepartmentService.insertHospitalDepartment(hospitalDepartment));
    }

    /**
     * 修改科室
     */
    @RequiresPermissions("peizhen:department:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        HospitalDepartment hospitalDepartment = hospitalDepartmentService.selectHospitalDepartmentById(id);
        mmap.put("hospitalDepartment", hospitalDepartment);
        return prefix + "/edit";
    }

    /**
     * 修改保存科室
     */
    @RequiresPermissions("peizhen:department:edit")
    @Log(title = "科室", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(HospitalDepartment hospitalDepartment)
    {
        return toAjax(hospitalDepartmentService.updateHospitalDepartment(hospitalDepartment));
    }

    /**
     * 删除
     */
    @RequiresPermissions("peizhen:department:remove")
    @Log(title = "科室", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") Long id)
    {
        return toAjax(hospitalDepartmentService.deleteHospitalDepartmentById(id));
    }

    /**
     * 选择科室树
     */
    @GetMapping(value = { "/selectDepartmentTree/{id}", "/selectDepartmentTree/" })
    public String selectDepartmentTree(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("hospitalDepartment", hospitalDepartmentService.selectHospitalDepartmentById(id));
        }
        return prefix + "/tree";
    }

    /**
     * 加载科室树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = hospitalDepartmentService.selectHospitalDepartmentTree();
        return ztrees;
    }
}
